package com.hrt.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.hrt.bean.HRTMember;
import com.hrt.bean.UserVO;
import com.hrt.bean.ViewResource;
import com.hrt.bean.ViewResourceScopes;
import com.hrt.manage.menus.service.ViewResourceService;
import com.hrt.util.WebParams;
import com.hrt.util.interceptor.RoleAccessInterceptor;

public class WebSessionInterceptor extends HandlerInterceptorAdapter implements
		RoleAccessInterceptor {

	private List<ViewResource> resList;

	@Autowired
	private ViewResourceService resService;;

	protected HttpServletRequest request;

	protected HttpServletResponse response;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String clzName = this.getClass().getName();
		resList = new ArrayList<ViewResource>();
		List<String> urlList = new ArrayList<String>();
		String packageName = this.getClass().getPackage().getName();
		System.out.println("class: " + clzName + "\n Package: " + packageName);

		this.request = request;
		this.response = response;
		String requestUri = request.getRequestURI();
		
		Cookie cookie=new Cookie(WebParams.MEMBER_PER_ACCESS, requestUri);
		cookie.setMaxAge(0);
		
		
		response.addCookie(cookie);
			
		
		request.getSession().setAttribute(WebParams.MEMBER_PER_ACCESS, requestUri);
		resList = resService.listManageMenuns(ViewResourceScopes.WEB_RESOURCE);

		// if (packageName.indexOf("com.hrt.web") != -1) {
		//
		// resList = resService.listManageMenuns(new Integer(1));
		// }
		if (resList != null && !resList.isEmpty()) {
			for (ViewResource res : resList) {
				urlList.add(res.getUrl());
			}
		}

		for (String url : urlList) {
		
				if (requestUri.indexOf(url) != -1) {
				
					if (isSessionTimeout(request, response)) {
						try {
							System.out.println("------------->"
									+ request.getContextPath());
							response.sendRedirect(request.getContextPath()
									+ "/HRTWEB/login.jsp");
							
							return false;

						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}

		return super.preHandle(request, response, handler);
	}

	@Override
	public boolean isSessionTimeout(
			HttpServletRequest request, HttpServletResponse response) {
	
			if (this.getHRTMember(request, response) == null) {

				return true;
			}

		return false;
	}

	protected UserVO getOnlineManager(HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getSession().getAttribute(WebParams.ON_LINE_MANAGER) != null) {
			return (UserVO) request.getSession().getAttribute(
					WebParams.ON_LINE_MANAGER);
		}

		return null;
	}

	protected HRTMember getHRTMember(HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getSession().getAttribute(WebParams.ON_LINE_MEMBER) != null) {

			return (HRTMember) request.getSession().getAttribute(
					WebParams.ON_LINE_MEMBER);
		}

		return null;
	}

	protected void setHRTMember(HRTMember member, HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getSession().getAttribute(WebParams.ON_LINE_MEMBER) != null) {
			request.getSession().removeAttribute(WebParams.ON_LINE_MEMBER);
		}
		request.getSession().setAttribute(WebParams.ON_LINE_MEMBER, member);

	}

	protected void setOnlineManager(UserVO userVo, HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		if (request.getSession().getAttribute(WebParams.ON_LINE_MANAGER) != null) {
			request.getSession().removeAttribute(WebParams.ON_LINE_MANAGER);
		}
		request.getSession().setAttribute(WebParams.ON_LINE_MANAGER, userVo);
	}
}
